package com.p7.notepad.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.p7.notepad.R;
import com.p7.notepad.adapter.NotepadAdapter;
import com.p7.notepad.app.BaseActivity;
import com.p7.notepad.bean.Notepad;
import com.p7.notepad.network.APIManager;
import com.p7.notepad.network.exception.ExceptionHandle;
import com.p7.notepad.network.listener.RequestListener;
import com.tools.s7.recycler.DividerItemDecoration;
import com.tools.s7.recycler.listener.Recycler;
import com.tools.s7.recycler.utils.LoadMoreHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class NotepadListActivity extends BaseActivity implements LoadMoreHelper.LoadDataListener, Recycler.OnItemClickListener<Notepad.DataBean> {

    @BindView(R.id.notepad_list_title)
    TextView mTitle;
    @BindView(R.id.notepad_list_data)
    RecyclerView mData;
    @BindView(R.id.notepad_list_srl)
    SwipeRefreshLayout mSrl;

    private LoadMoreHelper mHelper;
    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_notepad_list;
    }

    @Override
    protected void initEvents() {
        super.initEvents();

        Bundle bundle = getIntent().getBundleExtra("bundle");
        mTitle.setText( bundle.getString("title"));
        id = bundle.getString("id");

        mHelper = new LoadMoreHelper.Builder<Notepad.DataBean>()
                .setPageSize(20)
                .setSwipeRefresh(mSrl)
                .setRecycler(mData, new LinearLayoutManager(this), new DividerItemDecoration(this, 1, 0xFFDFDFDF))
                .setAdapter(new NotepadAdapter(), false)
                .setLoadData(this)
                .setItemClick(this)
                .build();
    }

    @Override
    public void onResume() {
        super.onResume();
        mHelper.loadFirstPage();
    }

    @OnClick(R.id.notepad_list_quit)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onLoadData(final int i) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("pageNum", String.valueOf(i));
        parameters.put("folderId", id);
        parameters.put("pageSize", String.valueOf(20));
        APIManager.onPost("notepad/getNotepadList", parameters, new RequestListener() {
            @Override
            public void onSuccess(String s) {
                Gson gson = new Gson();
                Notepad notepad = gson.fromJson(s, Notepad.class);
                if (notepad.getCode() == 1000) {
                    mHelper.notifyAdapter(notepad.getData());
                } else if (notepad.getCode() == 2001) {
                    startActivity(LoginActivity.class);
                } else {
                    if (i == 1) {
                        mHelper.notifyNull();
                    } else {
                        mHelper.notifyError();
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable error) {
                mHelper.notifyError();
            }
        });
    }

    @Override
    public void onItemClick(int i, Notepad.DataBean dataBean) {
        Bundle bundle = new Bundle();
        bundle.putString("id", String.valueOf(dataBean.getId()));
        startActivity(NotepadDetailsActivity.class, bundle);
    }
}
